package com.fy.common.model;

import com.fy.common.spring.LocaleMessageSource;
import com.fy.common.spring.SpringContextUtil;
import com.fy.common.enums.ErrorType;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel
public class ResponseResult<T> {
    @ApiModelProperty("响应结果码")
    private String code;
    @ApiModelProperty("服务器响应消息")
    private String message;
    @ApiModelProperty("服务器响应的业务数据")
    private T data;





    public static <T> ResponseResult ok() {
        return new ResponseResult(ErrorType.SUCCESS.getErrorCode(), ErrorType.SUCCESS.getErrorKey(), null);
    }

    public static <T> ResponseResult ok(T data) {
        return new ResponseResult(ErrorType.SUCCESS.getErrorCode(), ErrorType.SUCCESS.getErrorKey(), data);
    }

    public static <T> ResponseResult ok(String message, T t) {
        return new ResponseResult(ErrorType.SUCCESS.getErrorCode(), ErrorType.SUCCESS.getErrorKey(), t);
    }

    public static <T> ResponseResult ok(String code, String message, T t) {
        return new ResponseResult(code, message, t);
    }

    public static <T> ResponseResult fail() {
        LocaleMessageSource localeMessageSource = (LocaleMessageSource) SpringContextUtil.getBean("localeMessageSource");
        String message = localeMessageSource.getMessage("error.system");
        return new ResponseResult(ErrorType.SYSTEM_ERROR.getErrorCode(), ErrorType.SYSTEM_ERROR.getErrorKey(), null);
    }

    public static <T> ResponseResult fail(String message) {
        return new ResponseResult(ErrorType.SYSTEM_ERROR.getErrorCode(), message, null);
    }

    public static <T> ResponseResult fail(String code, String message) {
        return new ResponseResult(code, message, null);
    }

    public static <T> ResponseResult fail(String code, String message, T t) {
        return new ResponseResult(code, message, t);
    }

}
